<?php
$file = "./pages/configdemo";
$fhh = fopen($file,'r');
$dbdetails = explode(",",$_SESSION['dbdetails']);

$config = fread($fhh, filesize($file));
$config = str_replace("^DBNAME^",$dbdetails[1],$config);
$config = str_replace("^SERVERNAME^",$dbdetails[0],$config);
$config = str_replace("^USERNAME^",$dbdetails[2],$config);
$config = str_replace("^USERPASSWORD^",$dbdetails[3],$config);
fclose($fhh);
?>
<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>
    <div class="headh">
        <h3>Stage 3</h3>
    </div>
    <div class="contenth">
        <div>
		On to the final stage now we need to create your config script so the site knows how to access the database.
		<ul>
			<?php 
				fcflush();
				echo "<li>Checking ../system  is writable ";
				if (is_writable("../system/")) {
					echo "<span style=\"color:green\">OK</span>";
					$ok = 1;
				} else {
					echo '<span style=\"color:#FF0000\">FAIL</span>';
					$ok = 0;
				}
				echo "</li>";
				fcflush();
				echo "<li>Checking ../admin/system  is writable ";
				if (is_writable("../admin/system/")) {
					echo "<span style=\"color:green\">OK</span>";
					$ok = 1;
				} else {
					echo '<span style=\"color:#FF0000\">FAIL</span>';
					$ok = 0;
				}
				echo "</li>";
				fcflush();
				
				if ($ok==0) {
					echo "There were some problems with the permissions on the server we are attempting to fix them automatically<br /><br />";
					if (chmod("../system/",0777)) {
						$ok=1;
						echo "Fixed permissions on system directory<br /><br />";
					}
					if (chmod("../admin/system/",0777)) {
						$ok=1;
						echo "Fixed permissions on admin system directory<br /><br />";
					}
				}
		?>
		</ul>
		<?php
		echo "<strong>Creating and saving out config files</strong><br /><br />";
			
		if (file_exists("../system/config.inc.php")) {
			$ok=1;
		}
		if (file_exists("../admin/system/config.inc.php")) {
			$ok=1;
		}
		//write out normal config
			$fh = fopen("../system/config.inc.php","w");
			if (fwrite($fh, $config, strlen($config)) === FALSE) {
			 	$ok=0;
			} else {
			  	$ok=1;
			}
			fclose($fh);
		//write out admin config
			$fp = fopen("../admin/system/config.inc.php","w");
			if (fwrite($fp, $config, strlen($config)) === FALSE) {
			 	$ok=0;
			 } else {
			  	$ok=1;
			 }
			fclose($fp);
			if ($ok==0) {
				echo "We could not automatically create your config files.  Please copy and paste the contents of the text area below into a text file called config.inc.php<br /><br />Then using your FTP client upload this file into the /system/ and again into the /admin/system/ folders.<br /><br /><textarea cols=\"66\" rows=\"20\">$config</textarea>";
			}
			
			if ($ok==1) {
				echo "Config files have been created.  You can either complete the install process by clicking the arrow below or if you are reinstalling please delete the config.inc.php files and refresh this page.";
			}
		?>
		<br />
		</div>
    </div>
<b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b>
<?php if ($ok==1) { ?>
<a href="index.php?p=4"><img src="next.png" border="0" align="right" /></a>
<?php } ?>
